package library.view;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import library.dao.Impl.BookTypeDaoImpl;
import library.pojo.Book;
import library.service.LibraryService;

public class DeleteBookDialog extends JDialog {
	private Book book;
	private JTextField isbnTextField;
	private JTextField bookNameTextField;
	private JTextField authorTextField;
	private JTextField dateTextField;
	private JTextField bookStateTextField;
	private JTextField bookTypeTextField;
	public DeleteBookDialog(String title,Book book) {
		ImageIcon icon = new ImageIcon(MainJTPane.class.getResource("/title.png"));
		this.setIconImage(icon.getImage());
		this.book = book;
		this.setTitle(title);
		this.add(getBookPanel());
		this.setModal(true);
		this.setLocationRelativeTo(null);
		this.setVisible(true);
		this.validate();
	}

	private JPanel getBookPanel() {
		JPanel bookPanel = new JPanel();
		isbnTextField = new JTextField(15);
		bookNameTextField = new JTextField(15);
		authorTextField = new JTextField(15);
		dateTextField = new JTextField(15);
		bookStateTextField = new JTextField(15);
		bookTypeTextField = new JTextField(15);
		isbnTextField.setEditable(false);
		bookNameTextField.setEditable(false);
		authorTextField.setEditable(false);
		dateTextField.setEditable(false);
		bookStateTextField.setEditable(false);
		bookTypeTextField.setEditable(false);
		isbnTextField.setText(book.getIsbn());
		bookNameTextField.setText(book.getBookName());
		authorTextField.setText(book.getAuthor());
		dateTextField.setText(book.getPublicationYear());
		bookStateTextField.setText(book.getBookTypeIdx());
		bookTypeTextField.setText(book.getState());
		JButton confirmButton = new JButton("确定删除");
		confirmButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				updateActionPerformed(e);
			}
		});
		bookPanel.add(new JLabel("        isbn"));
		bookPanel.add(isbnTextField);
		bookPanel.add(new JLabel("        书名"));
		bookPanel.add(bookNameTextField);
		bookPanel.add(new JLabel("        作者"));
		bookPanel.add(authorTextField);
		bookPanel.add(new JLabel("出版日期"));
		bookPanel.add(dateTextField);
		bookPanel.add(new JLabel("图书类型"));
		bookPanel.add(bookTypeTextField);
		bookPanel.add(new JLabel("图书状态"));
		bookPanel.add(bookStateTextField);
		bookPanel.add(confirmButton)  ;
		this.setSize(270, 250);
	return bookPanel;
	}



	protected void updateActionPerformed(ActionEvent e) {
		LibraryService service = new LibraryService();
		service.deleteBook(book.getIsbn());
		this.dispose();
		JOptionPane.showMessageDialog(null, "删除成功！");
	}
}
